package com.lwy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwy.dao.BookNoveMapper;
import com.lwy.entity.BookNove;
import com.lwy.entity.BookNovelChapter;
import com.lwy.entity.BookUser;
import com.lwy.util.ChapterFilter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;

//小说表service接口实现类
@Service
public class BookNoveServiceImpl extends ServiceImpl<BookNoveMapper, BookNove> {

    @Resource
    private BookNovelChapterServiceImpl bookNovelChapterService;
    @Resource
    private BookUserServiceImpl bookUserService;

    public Integer hot(){
        Integer sum = 0;
        Integer count = baseMapper.count();
        Integer hot=0;
        List<BookNove> bookNoves = baseMapper.selectList(null);
        for (BookNove bookNove : bookNoves) {
            sum+=bookNove.getHits();
            hot = sum / count;
        }
        return hot;
    }

    //    简介
    public void getContent(BookNove bookNove){
        String content = bookNove.getContent();
        if(content.length()>30){
            String str = content.substring(0,30);
            str+="......";
            bookNove.setContent(str);
        }
    }

    public List<BookNove> hotList(){
        List<BookNove> hits = baseMapper.selectList(new QueryWrapper<BookNove>().gt("hits", hot()));
        for (BookNove hit : hits) {
            getContent(hit);
        }
        return hits;
    }
    //    按时间查小说
    public List<BookNove> newList(){
        String month = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
        List<BookNove> create_time = baseMapper.selectList(new QueryWrapper<BookNove>().like("create_time", month));
        for (BookNove bookNove : create_time) {
            getContent(bookNove);
        }
        return create_time;
    }
    //    综合查询
    public List<BookNove> completeList(){
        QueryWrapper<BookNove> wrapper = new QueryWrapper<>();
        wrapper.gt("hits",hot());
        wrapper.eq("serialize",1);
        List<BookNove> bookNoves = baseMapper.selectList(wrapper);
        for (BookNove bookNove : bookNoves) {
            getContent(bookNove);
        }
        return bookNoves;
    }

    public List<BookNove> findByList(Integer uid){
        return baseMapper.selectList(new QueryWrapper<BookNove>().eq("id", uid));

    }
    public List<BookNove> findByWork(Integer uid){
        List<BookNove> uid1 = baseMapper.selectList(new QueryWrapper<BookNove>().eq("uid", uid));
        for (BookNove bookNove : uid1) {
            BookNovelChapter bookNovelChapter = bookNovelChapterService.newList(bookNove.getId());
            if(!bookNovelChapter.getTitle().equals("暂无章节更新")){
                ChapterFilter.setEntity(bookNovelChapter);
            }
            bookNove.setBookNovelChapter(bookNovelChapter);
        }
        return uid1;
    }

    public BookNove findById(Integer id){
        BookNove byId = baseMapper.findById(id);
        String s = ChapterFilter.pageFormat(byId.getWord());
        byId.setWord(s);
        return byId;
    }

    public Object edit(Integer id) {
        BookNove bookNove = baseMapper.selectById(id);
        Integer hits = bookNove.getHits()+1;
        bookNove.setHits(hits);
        int i = baseMapper.updateById(bookNove);
        return i;
    }

    public Integer recommend(Integer id,Integer recommend,Integer uid) {
        BookNove bookNove = new BookNove();
        bookNove.setId(id);
        bookNove.setRecommend(recommend);
        int i = baseMapper.updateById(bookNove);
        BookUser bookUser = new BookUser();
        bookUser.setId(uid);
        bookUser.setRecommend(0);
        boolean b = bookUserService.updateById(bookUser);
        if(i>0&&b) {
            return 1;
        }else{
            return 2;
        }
    }

    public Integer up(Integer id,Integer up,Integer uid) {
        BookNove bookNove = new BookNove();
        bookNove.setId(id);
        bookNove.setUp(up);
        int i = baseMapper.updateById(bookNove);
        BookUser bookUser = new BookUser();
        bookUser.setId(uid);
        bookUser.setUp(0);
        boolean b = bookUserService.updateById(bookUser);
        if(i>0&&b){
            return 1;
        }else{
            return 2;
        }
    }
    public List<BookNove> AllFL(String title,Integer category,Integer serialize,String word,String hits,String create_time){
        String title1 =null;
        if(null!=title&&!"".equals(title)){
            title1= "%"+title+"%";
        }
        List<BookNove> aClass = baseMapper.findClass(title1, category, serialize, word, hits, create_time);
        for (BookNove bookNove : aClass) {
            getContent(bookNove);
        }
        return aClass;
    }

    public PageInfo<BookNove> page(String title,Integer pageNum, Integer pageSize){
        if(null==pageNum){
            pageNum=1;
        }if(null!=title&&!"".equals(title)){
            title ="%"+title+"%";
        }
        if(null==pageSize){
            pageSize=5;
        }
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<BookNove> pageInfo = new PageInfo<BookNove>(baseMapper.findName(title));
        return  pageInfo;
    }

    }
